package cn.sumpu.app.wlcity.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sumpu.app.wlcity.biz.VoteBiz;
import cn.sumpu.app.wlcity.dao.VoteDao;
import cn.sumpu.app.wlcity.domain.PageModel;
import cn.sumpu.app.wlcity.domain.Vote;
import cn.sumpu.app.wlcity.domain.VoteItem;
import cn.sumpu.app.wlcity.domain.VoteItemPhone;
import cn.sumpu.app.wlcity.exception.AppBusinessException;
import cn.sumpu.app.wlcity.util.Common;
import cn.sumpu.app.wlcity.util.IdGenerator;

public class VoteBizImpl implements VoteBiz {
	private VoteDao voteDao;

	public void setVoteDao(VoteDao voteDao) {
		this.voteDao = voteDao;
	}

	public void insertVote(Vote vote) throws AppBusinessException {
		voteDao.upEachVoteSort(vote.getApp());// 各项投票项递增1
		voteDao.insertVote(vote);

	}

	@Override
	public void deleteVote(String identifier) throws AppBusinessException {
		voteDao.deleteVote(identifier);
		voteDao.deleteVoteItemList(identifier);
	}

	@Override
	public List<Vote> getVoteAndVoteItemList(String app, Integer page)
			throws AppBusinessException {
		int pageSize = Common.PAGE_SIZE_VOTE;
		if (Common.empty(page)) {
			page = 1;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		map.put("app", app);
		return voteDao.getVoteAndVoteItemList(map);
	}

	@Override
	public void updateVote(Vote vote) throws AppBusinessException {
		voteDao.updateVote(vote);

	}

	@Override
	public Vote getVoteById(String id) throws AppBusinessException {
		return voteDao.getVoteById(id);
	}

	@Override
	public void insertVoteItem(VoteItem voteItem) throws AppBusinessException {
		voteDao.insertVoteItem(voteItem);

	}

	@Override
	public VoteItem getVoteItemById(String id) throws AppBusinessException {
		return voteDao.getVoteItemById(id);
	}

	@Override
	public void updateVoteItem(VoteItem voteItem) throws AppBusinessException {
		voteDao.updateVoteItem(voteItem);

	}

	@Override
	public void upEachVoteSort(String app) throws AppBusinessException {
		voteDao.upEachVoteSort(app);

	}

	// 上移
	@Override
	public void moveVoteSortUp(String voteId) throws AppBusinessException {
		Vote vote = voteDao.getVoteById(voteId);
		if (!Common.empty(vote)) {
			int nowSort = vote.getSort();
			if (nowSort > 1) {
				// 上面的移下去
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("originalSort", nowSort - 1);
				map.put("nowSort", nowSort);
				map.put("app", vote.getApp());
				voteDao.moveVoteSort(map);
				// 当前移上去
				vote.setSort(nowSort - 1);
				voteDao.updateVote(vote);
			}
		}

	}

	// 下移
	@Override
	public void moveVoteSortDown(String voteId) throws AppBusinessException {
		Vote vote = voteDao.getVoteById(voteId);
		if (!Common.empty(vote)) {
			int nowSort = vote.getSort();
			int last = voteDao.getVoteListCount(vote.getApp());
			// 下面的移上去
			if (nowSort < last) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("originalSort", nowSort + 1);
				map.put("nowSort", nowSort);
				map.put("app", vote.getApp());
				voteDao.moveVoteSort(map);
				// 当前移下去
				vote.setSort(nowSort + 1);
				voteDao.updateVote(vote);
			}
		}
	}

	@Override
	public int getVoteListCount(String app) throws AppBusinessException {

		return voteDao.getVoteListCount(app);
	}

	@Override
	public void closeVote(String voteId) throws AppBusinessException {
		Vote vote = new Vote();
		vote.setIdentifier(voteId);
		vote.setEnabled(false);
		voteDao.updateVote(vote);

	}

	@Override
	public void openVote(String voteId) throws AppBusinessException {
		Vote vote = new Vote();
		vote.setIdentifier(voteId);
		vote.setEnabled(true);
		voteDao.updateVote(vote);

	}

	@Override
	public void downEachVoteSort(Map<String, Object> map)
			throws AppBusinessException {
		voteDao.downEachVoteSort(map);

	}

	@Override
	public Boolean insertVoteUsePhone(String voteItemId, String phone,
			String app) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("phoneNumber", phone);
			map.put("voteItemId", voteItemId);
			if (app.equals(Common.APP_LIGHT)) {
				map.put("today", "today");
			}
			// VoteItemPhone isHasPhone=voteDao.checkVotePhone(map);
			// if(isHasPhone==null){
			VoteItemPhone voteItemPhone = new VoteItemPhone();
			voteItemPhone.setPhoneNumber(phone);
			voteItemPhone.setVoteItemId(voteItemId);
			voteDao.insertVoteItemPhone(voteItemPhone);

			VoteItem voteItem = voteDao.getVoteItemById(voteItemId);
			voteItem.setVotes(voteItem.getVotes() + 1);
			voteDao.updateVoteItem(voteItem);
			Vote vote = voteDao.getVoteById(voteItem.getVoteId());
			vote.setVoteCount(vote.getVoteCount() + 1);
			voteDao.updateVote(vote);
			return true;
		} catch (AppBusinessException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public List<Vote> getVoteList(Integer page) throws AppBusinessException {
		int pageSize = Common.PAGE_SIZE_VOTE;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		return voteDao.getVoteList(map);
	}

	@Override
	public List<VoteItem> getVoteItemList(String voteId, Integer page)
			throws AppBusinessException {
		int pageSize = Common.PAGE_SIZE_VOTE_ITEM;
		if (Common.empty(page)) {
			page = 1;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		map.put("voteId", voteId);
		return voteDao.getVoteItemList(map);
	}

	@Override
	public List<Vote> getFrontVoteList(Integer page, String app)
			throws AppBusinessException {
		int pageSize = Common.PAGE_SIZE_VOTE_FRONT;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		map.put("enabled", true);
		map.put("app", app);
		return voteDao.getVoteList(map);
	}

	@Override
	public PageModel getFrontVotePageModel(Integer page, String app)
			throws AppBusinessException {
		int pageSize = Common.PAGE_SIZE_VOTE_FRONT;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		map.put("enabled", true);
		map.put("app", app);
		List<Vote> list = voteDao.getVote(map);
		int count = voteDao.getVoteCount(map);
		PageModel pm = new PageModel();
		pm.setTotalRecord(count);
		pm.setPageSize(pageSize);
		pm.setCurrentPage(page);
		pm.setTotalPage((pageSize + count - 1) / pageSize);
		pm.setList(list);
		return pm;
	}

	@Override
	public void deleteVoteItem(String identifier) throws AppBusinessException {
		voteDao.deleteVoteItem(identifier);
		voteDao.deleteVoteItemPhone(identifier);
	}

	@Override
	public void deleteVoteAndChangeSort(String identifier, Integer sort,
			String app) throws AppBusinessException {
		voteDao.deleteVote(identifier);
		voteDao.deleteVoteItemList(identifier);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sort", sort);
		map.put("app", app);
		voteDao.downEachVoteSort(map);

	}

	@Override
	public int getVoteItemListCount(String parentVoteId)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("voteId", parentVoteId);
		return voteDao.getVoteItemListCount(map);
	}

	@Override
	public void saveVote(Vote vote) throws AppBusinessException {
		if (vote != null) {
			if (!Common.empty(vote.getIdentifier())) {
				voteDao.updateVote(vote);
			} else {
				vote.setIdentifier(IdGenerator.createIdentifier());
				vote.setSort(1);
				voteDao.upEachVoteSort(vote.getApp());// 各项投票项递增1
				voteDao.insertVote(vote);
			}
		}
	}

	@Override
	public void saveVoteItem(VoteItem voteItem) throws AppBusinessException {
		if (voteItem != null) {
			if (!Common.empty(voteItem.getIdentifier())) {
				voteDao.updateVoteItem(voteItem);
			} else {
				voteItem.setIdentifier(IdGenerator.createIdentifier());
				voteDao.insertVoteItem(voteItem);
			}
		}
	}

	@Override
	public int getVoteCountByPhoneDate(String phone, String date)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("date", date);
		map.put("phoneNumber", phone);
		return voteDao.getVotePhoneCount(map);
	}

	@Override
	public PageModel getVoteRank(Integer page, String voteType)
			throws AppBusinessException {
		PageModel pm = new PageModel();
		int pageSize = Common.PAGE_SIZE_VOTE;
		page = page == null ? 1 : page;
		Map<String, Object> mapVote = new HashMap<String, Object>();
		mapVote.put("enabled", true);
		mapVote.put("voteType", voteType);
		List<Vote> listVote = voteDao.getVote(mapVote);
		String voteIds = "";
		int length = listVote.size();
		for (int i = 0; i < length; i++) {
			String voteId = listVote.get(i).getIdentifier();
			if (i == 0 && length==1) {
				voteIds += "('" + voteId + "')";
			} else if (i == 0 && length>1) {
				voteIds += "('" + voteId + "',";
			} else if (i == length - 1) {
				voteIds += "'" + voteId + "')";
			} else {
				voteIds += "'" + voteId + "',";
			}
		}
		Map<String, Object> mapVoteItem = new HashMap<String, Object>();
		mapVoteItem.put("enabled", true);
		mapVoteItem.put("voteIds", voteIds);
		mapVoteItem.put("voteCount", "voteCount");
		mapVoteItem.put("startId", (page - 1) * pageSize);
		mapVoteItem.put("pageSize", pageSize);
		if(voteIds!="" && voteIds!=null){
			List<VoteItem> list=voteDao.getVoteItem(mapVoteItem);
			int count = voteDao.getVoteItemCount(mapVoteItem);
			pm.setTotalRecord(count);
			pm.setPageSize(pageSize);
			pm.setCurrentPage(page);
			pm.setTotalPage((pageSize + count - 1) / pageSize);
			pm.setList(list);
		}
		
		return pm;
	}

	@Override
	public PageModel getVote(Integer page, String app, String voteType)
			throws AppBusinessException {
		PageModel pm = new PageModel();
		int pageSize = Common.PAGE_SIZE_VOTE;
		page = page == null ? 1 : page;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		map.put("app", app);
		map.put("voteType", voteType);
		List<Vote> list = voteDao.getVote(map);
		int count = voteDao.getVoteCount(map);
		pm.setTotalRecord(count);
		pm.setPageSize(pageSize);
		pm.setCurrentPage(page);
		pm.setTotalPage((pageSize + count - 1) / pageSize);
		pm.setList(list);
		return pm;
	}

	@Override
	public PageModel getVoteItemLists(String voteId, Integer page)
			throws AppBusinessException {
		int pageSize = Common.PAGE_SIZE_VOTE_ITEM;
		if (Common.empty(page)) {
			page = 1;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		map.put("voteId", voteId);
		List<VoteItem> list = voteDao.getVoteItemList(map);
		int count = voteDao.getVoteItemListCount(map);
		PageModel pm = new PageModel();
		pm.setTotalRecord(count);
		pm.setPageSize(pageSize);
		pm.setCurrentPage(page);
		pm.setTotalPage((pageSize + count - 1) / pageSize);
		pm.setList(list);
		return pm;
	}
}
